package com.xiaoyang.task;

import com.xiaoyang.dto.UserDTO;
import com.xiaoyang.helper.AESHelper;
import com.xiaoyang.helper.DateHelper;
import com.xiaoyang.helper.GZIPHelper;
import com.xiaoyang.mapper.UserMapper;
import com.xiaoyang.pojo.User;
import com.xiaoyang.service.AutoLocationService;
import com.xiaoyang.service.UserService;
import com.xiaoyang.service.WxPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author guoziyang
 * @create 2020/9/21 10:18 下午
 */

@EnableScheduling
@Component
public class AutoSubmitTask {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private WxPushService wxPushService;

    @Autowired
    private AutoLocationService autoLocationService;

    /**
     * 星期二四六冲榜
     */
    @Scheduled(cron = "0 0 0 ? * TUE,THU,SAT")
    public void autoSubmitOn0() {
        autoSubmit();
    }

    /**
     * 生成日志
     */
    @Scheduled(cron = "0 1 0 ? * TUE,THU,SAT")
    public void logInfoOn0() {
        logInfo();
    }

    /**
     * 星期一三五七不冲
     */
    @Scheduled(cron = "0 0 9 ? * MON,WED,FRI,SUN")
    public void autoSubmitOn9() {
        autoSubmit();
    }

    /**
     * 生成日志
     */
    @Scheduled(cron = "0 1 9 ? * MON,WED,FRI,SUN")
    public void logInfoOn9() {
        logInfo();
    }

    private void autoSubmit() {
        List<User> users = userService.findAllUser();
        CyclicBarrier barrier = new CyclicBarrier(users.size());
        for (User user : users) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    UserDTO userDTO = new UserDTO();
                    userDTO.setUsername(AESHelper.decrypt(user.getUsername()));
                    userDTO.setPassword(AESHelper.decrypt(user.getPassword()));
                    userDTO.setUid(user.getUid());
                    userDTO.setExtension(GZIPHelper.uncompress(user.getExtension()));
                    String result = autoLocationService.submitLocation(userDTO);
                    Example example = new Example(User.class);
                    example.createCriteria()
                            .andEqualTo("username", user.getUsername());
                    user.setUpdateTime(new Date());
                    user.setResult(result);
                    userMapper.updateByExampleSelective(user, example);
                }
            }).start();
        }
        try {
            barrier.await(60, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void logInfo() {
        List<User> users = userService.findAllUser();
        List<User> userSort = users.stream().sorted(Comparator.comparing(User::getUpdateTime)).collect(Collectors.toList());
        StringBuffer sb = new StringBuffer();
        for (User user : userSort) {
            sb.append(user.getRealName() + DateHelper.formatDate(user.getUpdateTime()) + user.getResult() + "\n");
        }
        wxPushService.wxPush(sb.toString(), "UID_iAB4BFMt7quFBtA4eFOeQl117fbZ");
    }


}
